package kr.ac.kaist.ysproject;

import java.util.ArrayList;

import kr.ac.kaist.ysproject.data.DataProfile;
import kr.ac.kaist.ysproject.option.MainOptionMenu;
import kr.ac.kaist.ysproject.service.SmsSenderService;
import kr.ac.kaist.ysproject.util.DbManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends Activity {

	private ListView m_ListViewMain;
	private MainListAdapter m_adapter;
	private ArrayList<DataProfile> m_dataProfileArrList;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d("MainActivity", "OnCreate() started");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Splash(로딩화면) 띄우기
        startActivity(new Intent(this, SplashActivity.class));
        
        // Start SmsSenderService
        //Intent intent = new Intent();
        //intent.setClass("kr.ac.kaist.ysproject.service", "kr.ac.kaist.ysproject.service.SmsSenderService");
        startService(new Intent("kr.ac.kaist.ysproject.service.SmsSenderService"));
        
        ((Button) findViewById(R.id.buttonAddPerson)).setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 showDialog(0);
             }
         });
        

    }

    
    @Override
	public void onStart(){
		super.onStart();
		Log.d("onStart", "start");
		
		DbManager dbm = DbManager.getInstance(MainActivity.this);
		m_dataProfileArrList = dbm.getAllProfile();
		
		m_ListViewMain = (ListView)findViewById(R.id.listViewMain);
		
		m_adapter = new MainListAdapter(this, R.layout.main_list_row, m_dataProfileArrList);
		m_ListViewMain.setAdapter(m_adapter);
		
		m_adapter.notifyDataSetChanged();
		
    }
    
    
    
    
    /***********************************************************************
	 * 
	 * 
	 * 다이얼로그 정의 (대상추가를 전화번호부에서 찾을지, 직접 입력할지 선택)
	 * 
	 * 
	 ***********************************************************************/
    @Override
    protected Dialog onCreateDialog(int id) {
    	switch (id) {
	    	case 0:
		    	return new AlertDialog.Builder(MainActivity.this).setItems(R.array.dialog_items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
//					    String[] items = getResources().getStringArray(R.array.dialog_items);
//					    Toast.makeText(MainActivity.this, "\"" + which + "번 " + items[which] + "\"을 선택했습니다.", Toast.LENGTH_SHORT).show();
						
						// 전화번호부에서 대상 찾기
						if(which == 0) {
							Intent intent = new Intent("kr.ac.kaist.ysproject.ContactListActivity");
				            startActivity(intent);
						}
						
						// 직접 대상 입력하기
						else if(which == 1) {
							Intent intent = new Intent("kr.ac.kaist.ysproject.ProfileActivity");
				            startActivity(intent);
						}
					}
		        })
		        .create();
		    	
		    default :
		    	return null;
    	}
    }
    
    
    
    
    
    /***********************************************************************
	 * 
	 * 
	 * 옵션 메뉴
	 * 
	 * 
	 ***********************************************************************/
 	@Override
 	public boolean onPrepareOptionsMenu(Menu menu) {
 		new MainOptionMenu(this).initMenu(menu);	// 메뉴 시작시 작업이 필요할 경우
 		return super.onPrepareOptionsMenu(menu);
 	}

 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 		MenuInflater inflater = getMenuInflater();
 		inflater.inflate(R.menu.main_option_menu, menu);
 		return true;
 	}

 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		boolean tmp = new MainOptionMenu(this).initSelected(item);
 		if (tmp)
 			return tmp;
 		else
 			return super.onOptionsItemSelected(item);
 	}
}